package com.mia.props.client.container;

import com.mia.props.Props;
import com.mia.props.common.entities.TileDecobench;
import java.util.Iterator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.oredict.OreDictionary;

public class ContainerDecobench extends Container {
   public final EntityPlayer player;
   public final TileDecobench table;
   private int[] prevResources = new int[4];

   public ContainerDecobench(EntityPlayer player, TileEntity tileTable) {
      this.player = player;
      this.table = (TileDecobench)tileTable;
      this.addSlotToContainer(new Slot(this.table, 0, 10, 10) {
         public boolean isItemValid(ItemStack stack) {
            Iterator var2 = Props.allowableResourceItems.iterator();

            ItemStack allowedStack;
            do {
               if (!var2.hasNext()) {
                  return false;
               }

               allowedStack = (ItemStack)var2.next();
            } while(!OreDictionary.itemMatches(allowedStack, stack, false));

            return true;
         }
      });
      this.addSlotToContainer(new Slot(this.table, 1, 10, 120) {
         public boolean isItemValid(ItemStack stack) {
            return false;
         }
      });

      int x;
      for(x = 0; x < 3; ++x) {
         for(int y = 0; y < 9; ++y) {
            this.addSlotToContainer(new Slot(player.inventory, x + y * 9 + 9, 21 + x * 18, 141 + y * 18));
         }
      }

      for(x = 0; x < 9; ++x) {
         this.addSlotToContainer(new Slot(player.inventory, x, 21 + x * 18, 199));
      }

   }

   public boolean canInteractWith(EntityPlayer player) {
      return this.table.isUseableByPlayer(player);
   }

   public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
      ItemStack stack = null;
      Slot invSlot = this.getSlot(slot);
      if (invSlot != null && invSlot.getHasStack()) {
         ItemStack invStack = invSlot.getStack();
         stack = invStack.copy();
         if (slot < 2) {
            if (!this.mergeItemStack(invStack, 2, this.inventorySlots.size(), true)) {
               return null;
            }
         } else {
            boolean allowed = false;
            Iterator var7 = Props.allowableResourceItems.iterator();

            while(var7.hasNext()) {
               ItemStack allowedStack = (ItemStack)var7.next();
               if (OreDictionary.itemMatches(allowedStack, invStack, false)) {
                  allowed = true;
                  break;
               }
            }

            if (!allowed || !this.mergeItemStack(invStack, 0, 1, false)) {
               return null;
            }
         }

         if (invStack.stackSize == 0) {
            invSlot.putStack((ItemStack)null);
         } else {
            invSlot.onSlotChanged();
         }
      }

      return stack;
   }

   public void updateProgressBar(int index, int value) {
      this.table.setResource(index, value);
   }

   public void detectAndSendChanges() {
      super.detectAndSendChanges();

      for(int i = 0; i < 4; ++i) {
         if (this.table.getResource(i) != this.prevResources[i]) {
            this.prevResources[i] = this.table.getResource(i);

            for(int j = 0; j < this.crafters.size(); ++j) {
               ((ICrafting)this.crafters.get(j)).sendProgressBarUpdate(this, i, this.table.getResource(i));
            }
         }
      }

   }
}
